/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui2;

import entity.Customer;
import entity.ViewCart;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Windows7
 */
public class FrViewCart extends javax.swing.JFrame {

    private Customer customer;
    ArrayList<ViewCart> listCarts = new ArrayList<ViewCart>();

    /**
     * Creates new form FrViewCart
     */
    public FrViewCart(ArrayList<ViewCart> _listCart, Customer customer) {
        this.customer = customer;
        listCarts = _listCart;
        initComponents();
        this.setResizable(false);
        this.setTitle("Cart of customer");
        lblCustomer_Id.setText(String.valueOf(customer.getId()));
        lblValueUser.setText(customer.getUserName());
        this.setLocationRelativeTo(null);
        Date todayID = new Date(System.currentTimeMillis());
        SimpleDateFormat dayFomat = new SimpleDateFormat("yyyy-MM-dd");
        String today = dayFomat.format(todayID.getTime());
        lblDateValue.setText(today);
        table();
    }
    public GUIMenuCustomer frCusMeu;

    public void setFrCusMeu(GUIMenuCustomer frCusMeu) {
        this.frCusMeu = frCusMeu;
    }

    public void table() {
        ViewCart v = new ViewCart();
        String title[] = {"Menu_Id", "Menu", "Menu_Price", "Number", "Total"};
        Object data[][] = new String[listCarts.size() - 1][5];
        for (int i = 0; i < listCarts.size() - 1; i++) {
            v = listCarts.get(i);
            data[i][0] = String.valueOf(v.getMenu_Id());
            data[i][1] = v.getMenu();
            data[i][2] = String.valueOf(v.getMenuPrice());
            data[i][3] = String.valueOf(v.getNumber());
            data[i][4] = String.valueOf(v.getMenuPrice() * v.getNumber());
        }
        DefaultTableModel model = new DefaultTableModel(data, title) {
            //override phuong thuc này de cho phep table co the chinh sua duoc

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tblViewCart.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnOk = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        btnViewBill = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblViewCart = new javax.swing.JTable();
        lblDateValue = new javax.swing.JLabel();
        lblCustomer_Id = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        lblValueUser = new javax.swing.JLabel();
        lblViewCart = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setForeground(java.awt.Color.white);

        btnOk.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/ok.png"))); // NOI18N
        btnOk.setText("OK");
        btnOk.setPreferredSize(new java.awt.Dimension(100, 30));
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/delete_24.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.setPreferredSize(new java.awt.Dimension(100, 30));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnBack.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/back.png"))); // NOI18N
        btnBack.setText("Back");
        btnBack.setPreferredSize(new java.awt.Dimension(100, 30));
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        btnViewBill.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        btnViewBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/ViewBill.png"))); // NOI18N
        btnViewBill.setText("ViewBill");
        btnViewBill.setPreferredSize(new java.awt.Dimension(100, 30));
        btnViewBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewBillActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(100, 100, 100)
                .addComponent(btnViewBill, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(102, 102, 102)
                .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(170, 170, 170))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnViewBill, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        jLabel1.setText("Custome _Id :");
        jLabel1.setPreferredSize(new java.awt.Dimension(125, 25));

        lblDate.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        lblDate.setText("Date :");
        lblDate.setPreferredSize(new java.awt.Dimension(70, 25));

        tblViewCart.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblViewCart.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane2.setViewportView(tblViewCart);

        lblDateValue.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        lblDateValue.setText("*");
        lblDateValue.setPreferredSize(new java.awt.Dimension(50, 25));

        lblCustomer_Id.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        lblCustomer_Id.setText("*");
        lblCustomer_Id.setPreferredSize(new java.awt.Dimension(50, 25));

        lblUserName.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        lblUserName.setText("Customer_UserName :");
        lblUserName.setPreferredSize(new java.awt.Dimension(125, 25));

        lblValueUser.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        lblValueUser.setText("*");
        lblValueUser.setPreferredSize(new java.awt.Dimension(50, 25));

        lblViewCart.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        lblViewCart.setForeground(new java.awt.Color(102, 0, 102));
        lblViewCart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/cart_1.png"))); // NOI18N
        lblViewCart.setText(" VIEW CART");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblValueUser, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCustomer_Id, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblDate, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDateValue, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(108, 108, 108))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblViewCart, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(339, 339, 339))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 986, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblViewCart)
                        .addGap(333, 333, 333))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lblDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblDateValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblValueUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(lblCustomer_Id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(65, 65, 65)))
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        try {

            int Customer_Id = Integer.valueOf(lblCustomer_Id.getText());
            //   String Customer_UserName = lblValueUser.getText();
            //  java.sql.Date d = java.sql.Date.valueOf(lblDateValue.getText().toString());
            // java.sql.Date date = java.sql.Date.valueOf(txtDate.getText().toString());
            float tt = 0;
            for (int i = 0; i < listCarts.size(); i++) {
                float totalc = Float.valueOf(listCarts.get(i).getTotal());
                tt = totalc + tt;
            }
            CallableStatement callableStatement = management.MyConnection.getConnection().prepareCall("exec insertBill_Bill_Detail ?,?,?,?");
            callableStatement.setInt(1, Customer_Id);
            callableStatement.setString(2, lblDateValue.getText());
            callableStatement.setFloat(3, tt);
            callableStatement.registerOutParameter(4, java.sql.Types.INTEGER);
            callableStatement.execute();
            int bill_Id = callableStatement.getInt(4);
            System.out.println("bill_Id :" + bill_Id);
            for (int i = 0; i < listCarts.size() - 1; i++) {
                int id = Integer.valueOf(listCarts.get(i).getMenu_Id());
                int num = listCarts.get(i).getNumber();
                management.ManagerBillDetail.addBillDetail(bill_Id, id, num);
            }
            JOptionPane.showMessageDialog(this, "You buy :" + tt + "$");
        } catch (SQLException ex) {
            Logger.getLogger(FrViewCart.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_btnOkActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        String[] options = {"Yes", "No"};
        int choice = JOptionPane.showOptionDialog(this, "Do you want delete ?", "Massage", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        // choice == 0 có nghĩa là chọn có. mặc định ở thông báo đã hiên ra ở trên.
        // mặc định thứ tự idex của options yes là 0
        if (choice == 0) {
            try {
                //lấy ra dòng cần xóa
                int idx = tblViewCart.getSelectedRow();
                if(idx >= 0){
                listCarts.remove(idx);
                }
                table();
                JOptionPane.showMessageDialog(this, "Delete successfull", "Message", JOptionPane.QUESTION_MESSAGE);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnViewBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewBillActionPerformed
        // TODO add your handling code here:

        new FrViewBill().setVisible(true);
    }//GEN-LAST:event_btnViewBillActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /*
//         * Set the Nimbus look and feel
//         */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /*
//         * If Nimbus (introduced in Java SE 6) is not available, stay with the
//         * default look and feel. For details see
//         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(FrViewCart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(FrViewCart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(FrViewCart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(FrViewCart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /*
//         * Create and display the form
//         */
////        java.awt.EventQueue.invokeLater(new Runnable() {
////
////            public void run() {
////                new FrViewCart().setVisible(true);
////            }
////        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btnViewBill;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCustomer_Id;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblDateValue;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JLabel lblValueUser;
    private javax.swing.JLabel lblViewCart;
    private javax.swing.JTable tblViewCart;
    // End of variables declaration//GEN-END:variables
}
